package com.unknowpupil.cloud.consumer.account.controller;


import com.unknowpupil.cloud.common.dto.ResponseResult;
import com.unknowpupil.cloud.provider.account.api.IUnAccountService;
import com.unknowpupil.cloud.provider.account.domain.UnAccount;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户相关。
 *
 * @Classname: UnAccountController
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/3 23:41
 * @Description: controller 变成了业务逻辑层 CrossOrigin 跨域 maxAge超时时间
 * @Version: 1.0
 */
@CrossOrigin(origins = "*", maxAge = 36000)
@RestController
@RequestMapping("api/account")
public class UnAccountController {

    /**
     * 注册数据访问层
     */
    @Reference(version = "1.0.0")
    private IUnAccountService accountService;

    /**
     * 功能描述: <br>
     * 〈注册〉
     *
     * @param: unAccount {@link UnAccount}
     * @since: 1.0.0
     * @auther: w10g
     * @return: com.unknowpupil.cloud.common.dto.ResponseResult {@link ResponseResult}
     * @Date: 2020/1/4 1:13
     */
    @PostMapping("/reg")
    private ResponseResult<UnAccount> reg(@RequestBody UnAccount unAccount) {
        String message = validateReg(unAccount);
        //通过验证
        if(message==null) {
            int insert = accountService.insert(unAccount);
            UnAccount account = accountService.get(unAccount.getUserName());
            //注册成功
            if (insert > 0) {
                return new ResponseResult<UnAccount>(HttpStatus.OK.value(), "用户注册成功", account);
            }
        }
        return new ResponseResult<UnAccount>(HttpStatus.NOT_ACCEPTABLE.value(),message!=null?message:"用户注册失败");

    }

    /**
     * 功能描述: <br>
     * 〈 注册信息验证〉
     *
     * @param: unAccount {@link UnAccount}
     * @since: 1.0.0
     * @auther: w10g
     * @return: 验证信息
     * @Date: 2020/1/4 1:17
     */
    private String validateReg(UnAccount unAccount) {
        UnAccount account = accountService.get(unAccount.getUserName());
        if(account!=null){
            return "用户名已经存在，请重新输入";
        }
        return null;
    }

}
